using System;
using System.Text;
namespace etcetera.Mvc.UI.Toolbar
{
    public class ToolbarNavButton : ToolbarItem
    {
        private NavButton _navButton;

        public ToolbarNavButton(NavButton navButton)
        {
            _navButton = navButton;
        }

        public override string GetValue()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<div class='{0}'>", GetNavButtonClassName());

            PagerToolStrip toolStrip = (PagerToolStrip)Toolbar.ToolStrip;

            switch (_navButton)
            {
                case NavButton.First:
                    if (toolStrip.HasPreviousPage)
                    {
                        string url = toolStrip.GetPagingUrl(1);
                        sb.AppendLine(CreateSpanTag(url, toolStrip.FirstText));
                    }
                    else
                        sb.AppendLine("<span></span>");

                    break;

                case NavButton.Previous:
                    if (toolStrip.HasPreviousPage)
                    {
                        string url = toolStrip.GetPagingUrl(toolStrip.CurrentPage - 1);
                        sb.AppendLine(CreateSpanTag(url, toolStrip.PreviousText));
                    }
                    else
                        sb.AppendLine("<span></span>");

                    break;

                case NavButton.Next:
                    if (toolStrip.HasNextPage)
                    {
                        string url = toolStrip.GetPagingUrl(toolStrip.CurrentPage + 1);
                        sb.AppendLine(CreateSpanTag(url, toolStrip.NextText));
                    }
                    else
                        sb.AppendLine("<span></span>");

                    break;

                case NavButton.Last:
                    if (toolStrip.HasNextPage)
                    {
                        string url = toolStrip.GetPagingUrl(toolStrip.LastPage);
                        sb.AppendLine(CreateSpanTag(url, toolStrip.LastText));
                    }
                    else
                        sb.AppendLine("<span></span>");

                    break;

                default:
                    throw new ArgumentOutOfRangeException();
            }

            sb.Append("</div>");

            return sb.ToString();
        }

        private string GetNavButtonClassName()
        {
            string ret = "tBtn tBtn";

            PagerToolStrip toolStrip = (PagerToolStrip)Toolbar.ToolStrip;
            
            switch (_navButton)
            {
                case NavButton.First:
                    ret += "F";
                    if (!toolStrip.HasPreviousPage)
                        ret += " tBtnFd";
                    break;
                case NavButton.Previous:
                    ret += "P";
                    if (!toolStrip.HasPreviousPage)
                        ret += " tBtnPd";
                    break;
                case NavButton.Next:
                    ret += "N";
                    if (!toolStrip.HasNextPage)
                        ret += " tBtnNd";
                    break;
                case NavButton.Last:
                    ret += "L";
                    if (!toolStrip.HasNextPage)
                        ret += " tBtnLd";
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            return ret;
        }

    }
}